using System;
using System.Collections.Generic;
using System.Text;

namespace Nihonaid.classes
{
    partial class LearningItem : IComparable
    {
        /// <summary>
        /// Identifies the type of learning item
        /// </summary>
        public enum QuestionType : int
        {
            HiraganaReading = 5,
            KatakanaReading = 6,
            KanjiMeaning = 4,
            KanjiWordKana = 2,
            KanjiWordMeaning = 1,
            KanaWordMeaning = 3,
            Vocabulary = 7,
            EveryType = 8
        }

        /// <summary>
        /// Identifies the type of answer given by the user
        /// </summary>
        public enum AnswerType : int
        {
            Wrong,
            Correct
        }

        private int order;
        /// <summary>
        /// The item's position in the presentation order
        /// </summary>
        public int Order
        {
            get { return this.order; }
            set { this.order = value < 0 ? 0 : value; }
        }

        private int id;
        /// <summary>
        /// The item's unique identifier code
        /// </summary>
        public int ID
        {
            get { return this.id; }
            set { this.id = value < 0 ? 0 : value; }
        }

        private string question;
        /// <summary>
        /// The learning item's question
        /// </summary>
        public string Question
        {
            get { return this.question; }
            set { this.question = value; }
        }

        private string answer;
        /// <summary>
        /// The learning item's answer
        /// </summary>
        public string Answer
        {
            get { return this.answer; }
            set { this.answer = value; }
        }

        private DateTime last_repetition;
        /// <summary>
        /// The date and time of the last repetition of this item
        /// </summary>
        public DateTime LastRepetitionDate
        {
            get { return this.last_repetition > DateTime.Now ? DateTime.Now : this.last_repetition; }
            set { this.last_repetition = value > DateTime.Now ? DateTime.Now : value; }
        }

        private DateTime next_repetition;
        /// <summary>
        /// The schedule date and time for this item's next repetition
        /// </summary>
        public DateTime NextRepetitionDate
        {
            get { return this.next_repetition; }
            set { this.next_repetition = value <= DateTime.Now ? DateTime.Now : value; }
        }

        private int correct_streak;
        /// <summary>
        /// The number of correct answers in sequence
        /// </summary>
        public int CorrectStreak
        {
            get { return this.correct_streak < 0 ? 0 : this.correct_streak; }
            set { this.correct_streak = value < 0 ? 0 : value; }
        }

        private int correct_answers;
        /// <summary>
        /// The total number of correct answers
        /// </summary>
        public int CorrectAnswers
        {
            get { return this.correct_answers < 0 ? 0 : this.correct_answers; }
            set { this.correct_answers = value < 0 ? 0 : value; }
        }

        private int wrong_answers;
        /// <summary>
        /// The total number of wrong answers
        /// </summary>
        public int WrongAnswers
        {
            get { return this.wrong_answers < 0 ? 0 : this.wrong_answers; }
            set { this.wrong_answers = value < 0 ? 0 : value; }
        }

        private string mnemonic;
        /// <summary>
        /// This item's mnemonic aid
        /// </summary>
        public string Mnemonic
        {
            get { return (this.mnemonic == null) ? "" : this.mnemonic; }
            set { this.mnemonic = value == null ? "" : value; }
        }

        private LearningItem.QuestionType id_category;
        /// <summary>
        /// This item's category identification
        /// </summary>
        public LearningItem.QuestionType CategoryID
        {
            get { return this.id_category; }
            set { this.id_category = value; }
        }

        private string category_name;
        /// <summary>
        /// This item's category name
        /// </summary>
        public string CategoryName
        {
            get { return (this.category_name == null) ? "" : this.category_name; }
            set { this.category_name = (value == null) ? "" : value; }
        }

        #region IComparable Members
            
        public int CompareTo(object obj)
        {
            DateTime item1 = (DateTime)this.next_repetition;
            DateTime item2 = ((LearningItem)obj).next_repetition;
            int order1 = this.Order;
            int order2 = ((LearningItem)obj).Order;

            if (item1.Date < item2.Date)
                return -1;
            else
                if (item1.Date == item2.Date)
                {
                    if ((this.CorrectAnswers + this.WrongAnswers) == 0)
                    {
                        if (order1 < order2)
                            return -1;
                        else
                            return 1;
                    }
                    else
                    {
                        if (item1 < item2)
                            return -1;
                        else
                            return 1;
                    }
                }
                else
                    if (item1.Date > item2.Date)
                        return 1;
           return 0;
        }

        #endregion
    }
}
